package com.pengtu.web.asset.car;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.asset.car.Information;
import com.pengtu.service.asset.CarManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

//覆盖默认的nameSpace
@Namespace("/asset/car")
// 定义名为reload的result重定向到CarInfomation.action, 其他result则按照convention默认.
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "information!list.action", type = "redirect") })
public class InformationAction extends BaseActionSupport<Information> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private CarManager assetManager;

	private String id;
	private Information car;
	public Information getCar() {
		return car;
	}

	public void setCar(Information car) {
		this.car = car;
	}


	private List<Information> carInfoList;
	private Page<Information> page = new Page<Information>(10);

	
	public void setId(String id) {
		this.id = id;
	}

	public List<Information> getCarInfoList() {
		return carInfoList;
	}

	public Page<Information> getPage() {
		return page;
	}

	/**
	 * 检查车牌号是否已注册过
	 * @return
	 */
	public void findByLienNum() {

		String newCarLicen = Struts2Utils.getParameter("licenseplateNumber");
		String oldCarLicen = Struts2Utils.getParameter("oldCarLicen");
		
		if(assetManager.checkLienNum(newCarLicen,oldCarLicen)){
			Struts2Utils.renderText("true");
		}else{
			Struts2Utils.renderText("false");
		}
	}
	
	/**
	 * 检查编号是否重复
	 * @return
	 */
	public void findBySindex() {

		String newSindex = Struts2Utils.getParameter("sindex");
		String oldSindex = Struts2Utils.getParameter("oldSindex");
		
		if(assetManager.checkSindex(newSindex,oldSindex)){
			Struts2Utils.renderText("true");
		}else{
			Struts2Utils.renderText("false");
		}
	}
	
	/**
	 * 检查资产编号
	 * @return
	 */
	public void findByCode() {

		String newCode = Struts2Utils.getParameter("code");
		String oldCode = Struts2Utils.getParameter("oldCode");
		
		if(assetManager.checkCode(newCode,oldCode)){
			Struts2Utils.renderText("true");
		}else{
			Struts2Utils.renderText("false");
		}
	}
	
	/**
	 * 检查发动机号唯一
	 * @return
	 */
	public void findByEngineNo() {

		String newEngineNo = Struts2Utils.getParameter("engineNo");
		String oldEngineNo = Struts2Utils.getParameter("oldEngineNo");
		
		if(assetManager.checkEngineNo(newEngineNo,oldEngineNo)){
			Struts2Utils.renderText("true");
		}else{
			Struts2Utils.renderText("false");
		}
	}
	
	/**
	 * 检查车架号是否唯一
	 * @return
	 */
	public void findByFrameNo() {

		String newFrameNo = Struts2Utils.getParameter("frameNo");
		String oldFrameNo = Struts2Utils.getParameter("oldFrameNo");
		
		if(assetManager.checkFrameNo(newFrameNo,oldFrameNo)){
			Struts2Utils.renderText("true");
		}else{
			Struts2Utils.renderText("false");
		}
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("删除Id为"+id+"的车辆！");
		assetManager.deleteCar(id);
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}
	@Action("/asset/car/api/information")
	public String findNoUserCar() throws Exception{
		String startDate = Struts2Utils.getParameter("starttime");
		String endDate = Struts2Utils.getParameter("endtime");
		if(!startDate.equals("") && !endDate.equals("")){
		if(startDate.lastIndexOf(".")!= -1){
		startDate = startDate.substring(0, startDate.lastIndexOf("."));}
		if(endDate.lastIndexOf(".")!= -1){
		endDate = endDate.substring(0, endDate.lastIndexOf("."));}
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page =	assetManager.findNoUserCar(page,startDate,endDate);
		return SUCCESS;
	}
	@Override
	public String list() throws Exception {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = assetManager.searchPageCar(page, queryFilter);

		return SUCCESS;
	}
	
	@Override
	protected void prepareModel() throws Exception {

		if (StringUtils.isNotEmpty(id)) {
			car = assetManager.findById(id);
		} else {
			car = new Information();
		}
	}
	@Override
	public String save() throws Exception {
//		try {
//			String oldPrice  = Struts2Utils.getParameter("newPrice");
//			oldPrice = oldPrice.substring(1);
//			Double newPrice = new Double(oldPrice);
//			car.setBuyPrice(newPrice);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			car.setBuyPrice(0d);
//		}
		assetManager.saveCar(car);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"车牌号为"+car.getLicenseplateNumber()+"的车辆信息");
		return RELOAD;
	}
	
	@Autowired
	public void setAssetManager(CarManager assetManager) {
		this.assetManager = assetManager;
	}


	@Override
	public Information getModel() {
		return car;
	}
}
